package com.godme.rocket.repo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class GoodsRepository {


    @Value("${product.count}")
    public Integer productCount;

    public static final AtomicBoolean sellOut = new AtomicBoolean(false);


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void initCount(){
        stringRedisTemplate.opsForValue().set(productName, productCount.toString());
    }



    public void initCount(int total){
        stringRedisTemplate.opsForValue().set(productName, String.valueOf(total));
        sellOut.set(false);
    }



    public static final String productName = "hat";


    public void decreaseProduct(){
       stringRedisTemplate.opsForValue().decrement(productName);
    }

    public void incrementProduct(){
        sellOut.set(false);
        stringRedisTemplate.opsForValue().increment(productName);
    }

    public String residue(){
        sellOut.set(false);
        return stringRedisTemplate.opsForValue().get(productName);
    }
}
